#!/usr/bin/bash

# tmuxdump v0.1.0
# save tmux session's windows's path to $HOME/.tmux/tmuxsession_\$SESSION.windows
# Copyright by calvin 2021

function usage()
{
	echo "USAGE : tmuxdump [ session ]"
	exit 1
}

if [ $# -eq 1 ] && [ x"$1" = x"?" ] ; then
	usage
	exit 1
elif [ $# -gt 1 ] ; then
	usage
	exit 1
fi

SAVE_SESSION=$1

if [ ! -d $HOME/.tmux ] ; then
	mkdir -p $HOME/.tmux
fi

tmux list-sessions | tr ':' ' ' | awk '{print $1}' | while read SESSION ; do
	if [ x"$SAVE_SESSION" != x"" ] && [ x"$SAVE_SESSION" != x"$SESSION" ] ; then
		continue
	fi
	
	SAVEFILE="$HOME/.tmux/tmuxsession_$SESSION.windows"
	tmux list-windows -t "$SESSION" -F "#{window_index} #{pane_current_path}" >$SAVEFILE
	if [ $? -eq 0 ] ; then
		echo "tmux session $SESSION saved to $SAVEFILE"
	else
		echo "ERROR : tmux session $SESSION saving to $SAVEFILE failed"
	fi
done

